package config;

import cn.hutool.cron.CronUtil;
import com.jfinal.config.*;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.cron4j.Cron4jPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.server.undertow.UndertowServer;
import com.jfinal.template.Engine;
import common.model._MappingKit;
import service.TimingService;
import shiro.ShiroInterceptor;

public class WebConfig extends JFinalConfig {
    @Override
    public void configConstant(Constants constants) {
        PropKit.use("config.properties");
        constants.setEncoding("UTF-8");
        constants.setDevMode(true);
    }

    @Override
    public void configRoute(Routes routes) {
        routes.setBaseViewPath("/");
        routes.scan("controller.");
    }

    @Override
    public void configEngine(Engine engine) {

    }

    @Override
    public void configPlugin(Plugins plugins) {
        DruidPlugin dp=new DruidPlugin(PropKit.get("jdbcUrl"),PropKit.get("user"),PropKit.get("password"));
        ActiveRecordPlugin arp=new ActiveRecordPlugin(dp);
        _MappingKit.mapping(arp);
        plugins.add(dp);
        plugins.add(arp);
        CronUtil.start();
    }

    @Override
    public void configInterceptor(Interceptors interceptors) {
        interceptors.add(new ShiroInterceptor());
    }

    @Override
    public void configHandler(Handlers handlers) {

    }

    public static void main(String[] args) {
        UndertowServer.create(WebConfig.class,"undertow.properties")
                .configWeb(builder->{
                    builder.addFilter("encodingFilter","config.EncodingFilter");
                    builder.addFilterUrlMapping("encodingFilter","/*");
                    builder.addFilter("shiro","org.apache.shiro.web.servlet.ShiroFilter");
                    builder.addFilterUrlMapping("shiro","/*");
                    builder.addListener("org.apache.shiro.web.env.EnvironmentLoaderListener");
                }).start();
    }
}
